<?php

namespace app\admin\model;

use app\common\library\Auth;
use think\Model;

class Dish extends Model
{

    // 仅新增时自动设置「创建人」
    protected $insert = ['status' => '0'];

    // 表名
    protected $name = 'dish';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';

    // 定义时间戳字段名
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];

    // 字段类型或者格式转换
    protected $type = [
        'create_time' => 'datetime:Y-m-d H:i:s',
        'update_time' => 'datetime:Y-m-d H:i:s'
    ];

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ?: ($data['status'] ?? '');
        $list = $this->getStatusList();
        return $list[$value] ?? '';
    }

    public function flavors()
    {
        return $this->hasMany('DishFlavor');
    }


    public function category()
    {
        return $this->belongsTo('app\common\model\Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function setmeals()
    {
        return $this->belongsToMany('Setmeal', 'setmeal_dish');
    }
}
